package com.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 工具类
 * 
 * @author YH
 *
 */
public class MyTools {

	private static int num = 1;
	private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
	// 头像图片的目录
	public static final String HEAD_IMG_PATH = "headImg";

	// 时间格式化
	public static SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
	// 时间格式化2
	public static SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");
	// 时间格式化
	public static SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 获得指定长度的汉字
	 * @param length
	 * @return
	 */
	public static String getName(int length){
		String[] surnames={"王","李","张","刘","陈","杨","黄","周","赵","吴","孙","徐","林","胡","朱","郭","梁","马","高","何","郑","罗","宋","谢","叶","韩","任","潘","唐","于","冯","董","吕","邓","许","曹","曾","袁","程","田","彭","钟","蔡","魏","沈","卢","余","杜","蒋","汪","丁","方","苏","贾","姜","姚","陆","戴","傅","夏","廖","萧","石","范","金","谭","邹","崔","薛","邱","史","江","侯","邵","肖","熊","康","秦","雷","孟","白","毛","阎","郝","钱","段","俞","洪","顾","贺","龚","庞","尹","万","龙","赖","孔","武","邢","颜","汤","章","梅","常","阮"};
		String[] womenNames={"筠","柔","竹","霭","凝","晓","欢","霄","枫","芸","菲","寒","伊","亚","宜","可","姬","舒","影","荔","枝","思","丽","秀","娟","英","华","慧","巧","美","娜","静","淑","惠","珠","翠","雅","芝","玉","萍","红","娥","玲","芬","芳","燕","彩","春","菊","勤","珍","贞","莉","兰","凤","洁","梅","琳","素","云","莲","真","环","雪","荣","爱","妹","霞","香","月","莺","媛"," 艳","瑞","凡","佳","嘉","琼","桂","娣","叶","璧","璐","娅","琦","晶","妍","茜","秋","珊","莎","锦","黛","青","倩","婷","姣","婉","娴","瑾","颖","露","瑶","怡","婵","雁","蓓","纨","仪","荷","丹","蓉","眉","君","琴","蕊","薇","菁","梦","岚","苑","婕","馨","瑗","琰","韵","融","园","艺","咏","卿","聪","澜","纯","毓","悦","昭","冰","爽","琬","茗","羽","希","宁","欣","飘","育","滢","馥"};
		String[] menNames={"伟","刚","勇","毅","俊","峰","强","军","平","保","东","文","辉","力","明","永","健","世","广","志","义","兴","良","海","山","仁","波","宁","贵","福","生","龙","元","全","国","胜","学","祥","才","发","武","新","利","清","飞","彬","富","顺","信","子","杰","涛","昌","成","康","星","光","天","达","安","岩","中","茂","进","林","有","坚","和","彪","博","诚","先","敬","震","振","壮","会","思","群","豪","心","邦","承","乐","绍","功","松","善","厚","庆","磊","民","友","裕","河","哲","江","超","浩","亮","政","谦","亨","奇","固","之","轮","翰","朗","伯","宏","言","若","鸣","朋","斌","梁","栋","维","启","克","伦","翔","旭","鹏","泽","晨","辰","士","以","建","家","致","树","炎","德","行","时","泰","盛","雄","琛","钧","冠","策","腾","楠","榕","风","航","弘"};
		
		int flag=(int)(Math.random()*100);
		
		String str="";
		
		//姓
		str+=surnames[(int)(Math.random()*surnames.length)];
		
		
		if(flag>=50){
			//男
			for(int i=1;i<=length;i++){
				str+=womenNames[(int)(Math.random()*womenNames.length)];
			}
		}
		else{
			//女
			//男
			for(int i=1;i<=length;i++){
				str+=menNames[(int)(Math.random()*menNames.length)];
			}
		}
		return str;
	}
	
	/**
	 * 获得学校的名字
	 * @param length
	 * @return
	 */
	public static String getSchoolName(){
		String[] names={"清华大学","北京大学","北京化工大学","北京邮电大学","北京航空航天大学","北方交通大学","北京医科大学","北京农业大学","北京农业工程大学","北京工业大学","北京师范大学","北京中医药大学","中国地质大学","燕京研究院","北京教育学院","中国人民大学","北京协和医科大学","北京林业大学","石油大学(北京)","首都师范大学","首都经济贸易大学","中国矿业大学北京研究生部","中央音乐学院","北京语言文化大学","中央广播电视大学","北京广播学院","北京理工大学","北京外国语大学","北京联合大学","北京科技大学","北京体育大学","北方工业大学","中国青年政治学院","中国音乐学院","首都医科大学","对外经济贸易大学","外交学院","北京信息工程学院","中国政法大学","中美工商管理研修学院","北京东方大学","华北电力大学(北京)","天津大学","南开大学","天津财经学院","河北工业大学","天津医科大学","天津理工学院","天津商学院","天津城市建设学院","天津师范大学","天津广播电视大学","天津中医学院","天津武警指挥学院","天津市教育招生考试中心","天津旅游管理干部学院","天津美术学院","天津民用航空学院","天津企业管理培训中心","天津开发区国际学校","天津外国语学院","天津体育学院","天津职工大学","天津农学院","天津纺织工学院","天津音乐学院","上海交通大学","复旦大学","同济大学","华东工业大学","上海大学","上海电视大学","华东政法学院","华东师范大学","上海医科大学","上海第二医科大学","第二军医大学","上海对外贸易学院","上海铁道大学","中国纺织大学","华东理工大学","上海外国语学院","上海师范大学","上海农学院","上海纺织高等专科学校","上海中医药大学","上海财经大学","上海水产大学","重庆大学","重庆邮电学院","重庆建筑大学","第三军医大学","西南农业大学","重庆医科大学","西南政法大学","重庆交通学院","重庆后勤工程学院","西南师范大学","重庆师范学院","河北大学","石家庄铁道学院","燕山大学","石家庄邮政高等专科学校","河北师范大学","河北科技大学","邢台职业技术学院","河北医科大学","河北军械工程学院","石家庄二中","东北大学秦皇岛分校","石家庄职业技术学院","河北化工学校","河北省广播电视大学","河北经贸大学","华北煤炭医学院","河北鹿泉一中","河北建筑科技学院","郑州大学","焦作工学院","河南医科大学","安阳师范高等专科学校","郑州工业大学"};
		return names[(int)(Math.random()*names.length)];
	}
	
	
	/**
	 * 生成一个Id
	 * 
	 * @param length
	 *            id的长度，最低为20
	 * @return
	 */
	public static String getId(int length) {

		// 长度小于将长度改为16
		if (length < 16) {
			length = 16;
		}

		String string = "";

		// 获得当前的时间
		Date date = new Date();

		string = format1.format(date);

		// 生成随机的数
		for (int i = 1; i <= length - 14; i++) {
			string = string + (int) (Math.random() * 10);
		}

		return string;
	}


	

	/**
	 * 获得Time时间
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp getTimestamp(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = format.format(date);
		Timestamp time = Timestamp.valueOf(dateStr);
		return time;
	}

	/**
	 * 字符串转数字
	 * 
	 * @param str
	 * @param num
	 * @return
	 */
	public static int getNums(String str, int num) {
		Pattern pattern = Pattern.compile("[0-9]+");
		if (pattern.matcher(str).matches()) {
			num = Integer.parseInt(str);
		}
		return num;
	}

	/**
	 * 编码
	 * 
	 * @param str
	 * @return
	 */
	public static String encode(String str) {
		try {
			if (str == null) {
				str = "";
			}
			str = URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 解码
	 * 
	 * @param str
	 * @return
	 */
	public static String decode(String str) {
		try {
			if (str == null) {
				str = "";
			}
			str = URLDecoder.decode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 将异常的信息返回
	 * @param e
	 * @return
	 */
	public static String getException(Exception e){
		String err=e.getMessage()+":";
		StackTraceElement[] stackTrace = e.getStackTrace();
		for (StackTraceElement stackTraceElement : stackTrace) {
			err+=stackTraceElement+":";
		}
		return err;
	}

}
